//
//  EditDataController.m
//  MZJ
//
//  Created by jianglinfeng on 16/6/8.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "EditDataController.h"

@interface EditDataController ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{

    UIImageView *manbuttonImageView;
    UIImageView *womenbuttonImageView;
    UIImageView *yesbuttonImageView;
    UIImageView *nobuttonImageView;
    UIImageView *headimageV;
    UIAlertView *alertView_;
    UIImage *_faceImage;
}

@property (strong, nonatomic) IBOutlet UIButton *userHeadButton; //用户头像
@property (strong, nonatomic) IBOutlet UITextField *nickNameTextfield; //昵称输入
@property (strong, nonatomic) IBOutlet UIImageView *nickImageView;
@property (strong, nonatomic) IBOutlet UIButton *manButton; //男士
@property (strong, nonatomic) IBOutlet UIButton *womenButton; //女士
@property (strong, nonatomic) IBOutlet UITextField *ageTextfield; //年龄输入
@property (strong, nonatomic) IBOutlet UIImageView *ageImageView;
@property (strong, nonatomic) IBOutlet UITextField *nameTextfield;//真实姓名
@property (strong, nonatomic) IBOutlet UIImageView *nameImageView;
@property (strong, nonatomic) IBOutlet UITextField *uCardTextField;//身份证号码输入
@property (strong, nonatomic) IBOutlet UIImageView *ucardImageView;
@property (strong, nonatomic) IBOutlet UILabel *phoneNumLabel;//手机号码显示
@property (strong, nonatomic) IBOutlet UITextField *addressTextfield;//地址输入
@property (strong, nonatomic) IBOutlet UIImageView *addressImageView;
@property (strong, nonatomic) IBOutlet UIButton *yesButton;// 是
@property (strong, nonatomic) IBOutlet UIButton *noButton; //否
@property (strong, nonatomic) IBOutlet UIButton *confirmButton; //点击确认
@property (strong, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic, strong) NetWorkingSModel *model;
@property (nonatomic, strong) NSMutableDictionary *dataEdit_dic; //保存数据dic

@end

@implementation EditDataController

- (void)dealloc {

    NSLog(@"editDataController dealloc");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    self.topBar.backgroundColor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:10/255.0 alpha:1];
    self.titleLabel.text = @"完善资料";
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [self.backButton setTitle:@"" forState:UIControlStateNormal];
    self.dataEdit_dic = [NSMutableDictionary dictionary];
    [self setUpView];
    
    self.view.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickViewAction:)];
    [self.view addGestureRecognizer:singleTap];
    
    if (!_model) {
        _model = [[NetWorkingSModel alloc] init];
    }
}

- (void)clickedBackButton:(UIButton *)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickViewAction:(UITapGestureRecognizer *)sender {

    [self.nickNameTextfield resignFirstResponder];
    [self.ageTextfield resignFirstResponder];
    [self.nameTextfield resignFirstResponder];
    [self.uCardTextField resignFirstResponder];
    [self.addressTextfield resignFirstResponder];
}

- (void)setUpView {

    self.nickNameTextfield.delegate = self;
    self.ageTextfield.delegate = self;
    self.nameTextfield.delegate = self;
    self.uCardTextField.delegate = self;
    self.addressTextfield.delegate = self;
    
    self.ageTextfield.keyboardType = UIKeyboardTypeNumberPad;
    self.uCardTextField.keyboardType = UIKeyboardTypeURL;
    
    
    self.phoneNumLabel.text = [DataModel object].userDetail[@"umobile"];
    self.phoneNumLabel.font = [UIFont systemFontOfSize:14];
    self.phoneNumLabel.textColor = [UIColor grayColor];
    
    self.userHeadButton.layer.cornerRadius = 25.0;
    self.userHeadButton.layer.masksToBounds = YES;
//    self.userHeadButton.backgroundColor = [UIColor orangeColor];
    [self.userHeadButton setTitle:@"" forState:UIControlStateNormal];
    headimageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    headimageV.layer.cornerRadius = 25.0;
    headimageV.layer.masksToBounds = YES;
    [headimageV sd_setImageWithURL:[NSURL URLWithString:[DataModel object].userDetail[@"uheadimg"]]
                          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    [self.userHeadButton addSubview:headimageV];
    [self.userHeadButton addTarget:self action:@selector(userHeadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.manButton setTitle:@"" forState:UIControlStateNormal];
    manbuttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    manbuttonImageView.contentMode = UIViewContentModeScaleToFill;
    manbuttonImageView.center = CGPointMake(manbuttonImageView.center.x, 15);

    [self.manButton addSubview:manbuttonImageView];
    UILabel *manLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(manbuttonImageView.frame), 0, 30, 30)];
    manLabel.text = @"男";
    manLabel.font = [UIFont systemFontOfSize:14];
    manLabel.textAlignment = NSTextAlignmentCenter;
    [self.manButton addSubview:manLabel];
    
    [self.womenButton setTitle:@"" forState:UIControlStateNormal];
    womenbuttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    womenbuttonImageView.center = CGPointMake(womenbuttonImageView.center.x, 15);
    [self.womenButton addSubview:womenbuttonImageView];
    UILabel *womenLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(womenbuttonImageView.frame), 0, 30, 30)];
    womenLabel.text = @"女";
    womenLabel.font = [UIFont systemFontOfSize:14];
    womenLabel.textAlignment = NSTextAlignmentCenter;
    [self.womenButton addSubview:womenLabel];
    
    [self.yesButton setTitle:@"" forState:UIControlStateNormal];
    yesbuttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    yesbuttonImageView.center = CGPointMake(yesbuttonImageView.center.x, 15);
    [self.yesButton addSubview:yesbuttonImageView];
    UILabel *yesLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yesbuttonImageView.frame), 0, 30, 30)];
    yesLabel.text = @"是";
    yesLabel.font = [UIFont systemFontOfSize:14];
    yesLabel.textAlignment = NSTextAlignmentCenter;
    [self.yesButton addSubview:yesLabel];
    
    [self.noButton setTitle:@"" forState:UIControlStateNormal];
    nobuttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    nobuttonImageView.center = CGPointMake(nobuttonImageView.center.x, 15);
    [self.noButton addSubview:nobuttonImageView];
    UILabel *noLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nobuttonImageView.frame), 0, 30, 30)];
    noLabel.text = @"否";
    noLabel.font = [UIFont systemFontOfSize:14];
    noLabel.textAlignment = NSTextAlignmentCenter;
    [self.noButton addSubview:noLabel];
    
    self.confirmButton.backgroundColor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:10/255.0 alpha:1];
    self.confirmButton.layer.cornerRadius = 4.0;
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //button添加点击事件
    [self.manButton addTarget:self action:@selector(manButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.womenButton addTarget:self action:@selector(womenButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.yesButton addTarget:self action:@selector(yesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.noButton addTarget:self action:@selector(noButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.manButton.tag = 10;
    self.womenButton.tag = 11;
    self.yesButton.tag = 12;
    self.noButton.tag = 13;
    
    //设置选择button的图片
    //进来此页面先判断后台的数据
    NSString *sexString = [[DataModel object].userDetail objectForKey:@"usex"];
    if ([sexString integerValue] == 0) {
        
        //保密
        manbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        womenbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"0" forKey:@"usex"];
    }
    if ([sexString integerValue] == 1) {
        
        //男
        manbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
        womenbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"1" forKey:@"usex"];
    }
    if ([sexString integerValue] == 2) {
        
        //女
        womenbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
        manbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"2" forKey:@"usex"];
    }
    NSString *utypeString = [[DataModel object].userDetail objectForKey:@"utype"];
    if ([utypeString integerValue] == 0) {
        
        //不是
        nobuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
        yesbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"0" forKey:@"utype"];
    }
    if ([utypeString integerValue] == 1) {
        
        //是
        yesbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
        nobuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"1" forKey:@"utype"];
    }
    if ([utypeString integerValue] == 2) {
        
        //未设置
        yesbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        nobuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
        [self.dataEdit_dic setObject:@"2" forKey:@"utype"];
    }
    
    //通过判断本地是否有信息设置textfield的placeHolder
    if (IsNilString([[DataModel object].userDetail objectForKey:@"unickname"])) {
        
        self.nickNameTextfield.placeholder = @"请输入昵称...";
    }
    else {
    
        self.nickNameTextfield.text = [[DataModel object].userDetail objectForKey:@"unickname"];
    }
    
    if (IsNilString([[DataModel object].userDetail objectForKey:@"uage"])) {
        self.ageTextfield.placeholder = @"请输入年龄...";
    }
    else {
    
        self.ageTextfield.text = [[DataModel object].userDetail objectForKey:@"uage"];
    }
    
//    if (IsNilString([[DataModel object].userDetail objectForKey:@"ucompellation"])) {
//        
//        self.nameTextfield.placeholder = @"请输入真实姓名...";
//    }
    NSString *ss = [NSString stringWithFormat:@"%@",[DataModel object].userDetail[@"ucompellation"]];
    NSLog(@"%d",[ss intValue]);
    if ([ss intValue] == 0) {
        
        self.nameTextfield.placeholder = @"请输入真实姓名...";
    }
    else {
    
        self.nameTextfield.text = [[DataModel object].userDetail objectForKey:@"ucompellation"];
    }
    
     NSString *sss = [NSString stringWithFormat:@"%@",[DataModel object].userDetail[@"ucard"]];
    if ([sss intValue] == 0) {
        self.uCardTextField.placeholder = @"请输入身份证号码...";
    }
    else {
    
        self.uCardTextField.text = [[DataModel object].userDetail objectForKey:@"ucard"];
    }
    
    NSString *ssss = [NSString stringWithFormat:@"%@",[DataModel object].userDetail[@"uaddress"]];
    if ([ssss intValue] == 0) {
        
        self.addressTextfield.placeholder = @"请输入地址...";
    }
    else {
    
        self.addressTextfield.text = [[DataModel object].userDetail objectForKey:@"uaddress"];
    }
    
    //监听键盘弹出与收回
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark --button click

- (void)manButtonClick:(UIButton *)sender {

    manbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
    womenbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
    [self.dataEdit_dic setObject:@"1" forKey:@"usex"];
}

- (void)womenButtonClick:(UIButton *)sender {

    womenbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
    manbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
    [self.dataEdit_dic setObject:@"2" forKey:@"usex"];
}

- (void)yesButtonClick:(UIButton *)sender {

    nobuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
    yesbuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
    [self.dataEdit_dic setObject:@"1" forKey:@"utype"];
}

- (void)noButtonClick:(UIButton *)sender {
    
    nobuttonImageView.image = [UIImage imageNamed:@"Checked_YES_img@2x"];
    yesbuttonImageView.image = [UIImage imageNamed:@"Checked-NO_img@2x"];
    [self.dataEdit_dic setObject:@"0" forKey:@"utype"];
}

//点击用户头像
- (void)userHeadButtonClick:(UIButton *)sender {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //用户相册
        [self takePhotoPicker:UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //拍照
        [self takePhotoPicker:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)takePhotoPicker:(UIImagePickerControllerSourceType)sourceType {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:picker animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image= [info objectForKey:@"UIImagePickerControllerEditedImage"];
    _faceImage = image;
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        //如果是拍照，保存到相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    //上传头像
    NSData *formData = UIImagePNGRepresentation(image);
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *token = [user objectForKey:@"user_token"];
    [dic setObject:token forKey:@"token"];
    
    [NetRequestClass NetUploadFilesPOSTWithRequestURL:headimg withParameter:dic data:formData compressData:imageData name:@"headeimg" fileName:@"file.png" mimeType:@"image/png" WithReturnValeuBlock:^(id returnValue) {
       NSData *data =[returnValue dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary* dict_ = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if ([dict_[@"statu"] intValue] == 0) {
            headimageV.image = _faceImage;
        } else {
            [self waringAction:dict_[@"result"]];
        }
        
        
        NSLog(@"头像上传 = %@",dict_);
    } WithErrorCodeBlock:^(id errorCode) {
        
        NSLog(@"上传失败");
        
    } WithFailureBlock:^{
        
        NSLog(@"上传失败");
    }];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//图片压缩到指定大小
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
    UIImage *sourceImage = self.userHeadButton.imageView.image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)confirmButtonClick:(UIButton *)sender {

    [self.nickNameTextfield resignFirstResponder];
    [self.ageTextfield resignFirstResponder];
    [self.nameTextfield resignFirstResponder];
    [self.uCardTextField resignFirstResponder];
    [self.addressTextfield resignFirstResponder];
    
    if (IsNilString(self.ageTextfield.text) || IsNilString(self.nameTextfield.text) || IsNilString(self.uCardTextField.text) || IsNilString(self.addressTextfield.text) || [self.dataEdit_dic[@"usex"] intValue] == 0){
        [self waringAction:@"请完善信息!"];
    } else {
        __weak typeof (self)weakSelf = self;
        
        //    [weakSelf.dataEdit_dic setObject:sex_Str forKey:@"usex"];
        
        //    [weakSelf.dataEdit_dic setObject:volunteers_Str forKey:@"utype"];
        [self.dataEdit_dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_token"] forKey:@"token"];
        
        [_model setBlockWithReturnBlock:^(id returnValue) {
            
            NSLog(@"上传个人资料 = %@",returnValue);
            [weakSelf.navigationController popViewControllerAnimated:YES];
            weakSelf.tabBarController.tabBar.hidden = NO;
            
        } WithErrorBlock:^(id errorCode) {
            [weakSelf waringAction:errorCode[@"result"]];
        }];
        [weakSelf.model userGetEdit_Userinfo:weakSelf.dataEdit_dic];
    }
    NSLog(@"点击确定,保存用户设置的信息");
    
    NSLog(@"保存的数据：%@",self.dataEdit_dic);
}

#pragma mark --textfieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {

    if (textField == self.nickNameTextfield) {
        
        [self.dataEdit_dic setObject:self.nickNameTextfield.text forKey:@"unickname"];
    }
    if (textField == self.ageTextfield) {
        
        [self.dataEdit_dic setObject:self.ageTextfield.text forKey:@"uage"];
    }
    if (textField == self.nameTextfield) {
        
        [self.dataEdit_dic setObject:self.nameTextfield.text forKey:@"ucompellation"];
    }
    if (textField == self.uCardTextField) {
        
        if (self.uCardTextField.text.length == 16 || self.uCardTextField.text.length == 18) {
            
            [self.dataEdit_dic setObject:self.uCardTextField.text forKey:@"ucard"];
        }
        else {
        
            [self alertViewShow:@"请输入正确的身份证号码"];
        }
    }
    if (textField == self.addressTextfield) {
        
        [self.dataEdit_dic setObject:self.addressTextfield.text forKey:@"uaddress"];
    }
}

- (void)alertViewShow:(NSString *)message {

    if (!alertView_) {
        
        alertView_ = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    }
    
    [alertView_ show];
}

- (void)keyboardWillShow:(NSNotification *)notification {

    // 获取必要信息
    if (self.nameTextfield.isEditing || self.uCardTextField.isEditing || self.addressTextfield.isEditing) {
        
        NSValue *keyboardFrameValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGFloat keyboardTop = keyboardFrameValue.CGRectValue.origin.y;
        NSTimeInterval duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
        
        // 添加移动动画，使视图跟随键盘移动
        [UIView animateWithDuration:duration animations:^{
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationCurve:curve];
            CGRect frame = self.contentView.frame;
            frame = CGRectMake(0, keyboardTop - frame.size.height + 100, self.view.frame.size.width, frame.size.height);
            self.contentView.frame = frame;
        }];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    // 获取必要信息
    NSTimeInterval duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    // 添加移动动画，使视图跟随键盘移动
    [UIView animateWithDuration:duration animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:curve];
        CGRect frame = self.contentView.frame;
        frame.origin.y = 0;
        self.contentView.frame = frame;
    }];
}

- (void)didReceiveMemoryWarning {
   
    [super didReceiveMemoryWarning];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    self.tabBarController.tabBar.hidden = NO;
}

@end
